﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using WeiboPlus.Models;
using System.Threading;
using WeiboPlus.Service;
using WeiboPlus;
using Microsoft.Practices.Prism.Events;
using WeiboPlus.ViewModel;

namespace WeiboPlus.Shell.ViewModel
{
    public class SearchViewModel : CollectionVMBase<SNSStatusViewModel>
    {

        IStatusService _statusSvc;
        //private IEventAggregator _ev;

        ObservableCollection<SNSTrendsViewModel> trendsByHour = new ObservableCollection<SNSTrendsViewModel>();

        public ObservableCollection<SNSTrendsViewModel> TrendsByHour
        {
            get { return trendsByHour; }
            set
            {
                trendsByHour = value;
                RaisePropertyChanged("TrendsByHour");
            }
        }

        ObservableCollection<SNSTrendsViewModel> trendsByDay = new ObservableCollection<SNSTrendsViewModel>();

        public ObservableCollection<SNSTrendsViewModel> TrendsByDay
        {
            get { return trendsByDay; }
            set
            {
                trendsByDay = value;
                RaisePropertyChanged("TrendsByDay");
            }
        }

        ObservableCollection<SNSTrendsViewModel> trendsByWeek = new ObservableCollection<SNSTrendsViewModel>();

        public ObservableCollection<SNSTrendsViewModel> TrendsByWeek
        {
            get { return trendsByWeek; }
            set
            {
                trendsByWeek = value;
                RaisePropertyChanged("TrendsByWeek");
            }
        }

        int pivotSelectedIndex;

        public int PivotSelectedIndex
        {
            get { return pivotSelectedIndex; }
            set
            {
                pivotSelectedIndex = value;
                RaisePropertyChanged("PivotSelectedIndex");
            }
        }

        string searchKeyword;

        public string SearchKeyword
        {
            get { return searchKeyword; }
            set
            {
                searchKeyword = value;

                //if (PivotSelectedIndex != 4)
                //{
                //    PivotSelectedIndex = 4;
                //}

                RaisePropertyChanged(() => SearchKeyword);
            }
        }

        public SearchViewModel()
        {
            _statusSvc = PhoneApplicationController.Current.Container.Resolve<IStatusService>();
        }

        protected override void Initialize()
        {
            //base.Initialize();
            ThreadPool.QueueUserWorkItem((o) =>
            {
                GetTrends();
            });
            Loading = true;
        }

        private void GetTrends()
        {
            SNSRequest req1 = new SNSRequest() { Type = SNSType.TrendsByHour };
            _statusSvc.GetTrends(req1, (t, r) =>
            {
                if (!r.IsSuccess)
                {
                    ExecuteOnUIThread(() =>
                    {
                        MessageBox.Show(r.Message);
                    });
                }
                else
                {
                    if (t != null && t.Trends.Count > 0)
                    {
                        var trend = new SNSTrendsViewModel(t)
                        {
                            Provider = AccountManager.Current.GetAccount(r.AccountID).ServiceProvider
                        };
                        ExecuteOnUIThread(() =>
                        {
                            TrendsByHour.Add(trend);
                        });
                    }
                }
                ExecuteOnUIThread(() =>
                {
                    Loading = false;
                });
            });
            //throw new NotImplementedException();
        }
    }
}
